package share.storage.io.basic;

import java.io.FileNotFoundException;
import java.io.IOException;

import share.storage.io.File;
import share.storage.io.FileReader;
import share.storage.io.FileType;

public class BasicFile implements File {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5844256496694487726L;
	java.io.File file = null;
	File parent = null;
	String filename = null;
	String describe = "";
	
	public BasicFile(String path) {
		file = new java.io.File(path);
	}

	public BasicFile(java.io.File file) {
		this.file = file;
	}

	public long getSize() {
		return file.length();
	}

	public boolean isDirectory() {
		return file.isDirectory();
	}

	public boolean isFile() {
		return file.isFile();
	}

	public File[] getChildren() {
		if (!file.isDirectory())
			return null;
		java.io.File[] files = file.listFiles();
		File[] children = new File[files.length];
		for (int i = 0; i < children.length; i++) {
			children[i] = new BasicFile(files[i]);
			children[i].setParent(this);
		}
		return children;
	}

	public String getPath() {
		try {
			return file.getCanonicalPath();
		} catch (IOException e) {
			return file.getAbsolutePath();
		}
	}

	public FileReader getReader() {
		try {
			return new BasicFileReader(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		}
	}

	public String getFilename() {
		if (filename != null)
			return filename;
		return file.getName();
	}

	public File getParent() {
		if (parent != null)
			return parent;
		java.io.File _parent = file.getParentFile();
		if (_parent == null)
			return null;
		File parent = new BasicFile(_parent);
		return parent;
	}

	public void setParent(File parent) {
		this.parent = parent;
	}

	public void setFileName(String name) {
		this.filename = name;
	}

	public File findChildByName(String name) {
		if(name.contains("/")||name.equals(".."))
			return null;
		File file=new BasicFile(getPath()+"/"+name);
		file.setParent(this);
		return file;
	}

	public boolean isHidden() {
		if(file.isHidden()||file.getName().startsWith("."))
			return true;
		return false;
	}

	public String getDescribe() {
		return describe;
	}

	public long lastModified() {
		return file.lastModified();
	}

	public void setDescribe(String describe) {
		// TODO Auto-generated method stub

	}

	public FileType getFileType() {
		return FileType.FILE;
	}


	@Override
	public void setFileType(FileType type) {
		// TODO Auto-generated method stub
		
	}

}
